home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / term / extras / source / gtlayout-source.lha / LTP_GlyphSetup.c < prev    next >
C/C++ Source or Header  |  1995-03-31  |  3KB  |  177 lines

  1. /*  GadTools layout toolkit
  2. **
  3. **  Copyright © 1993-1995 by Olaf `Olsen' Barthel
  4. **  Freely distributable.
  5. */
  6.  
  7. #include "gtlayout_global.h"
  8.  
  9. VOID __regargs
  10. LTP_GetDefaultFont(struct TTextAttr *TextAttr)
  11. {
  12.     struct TextFont *Font = GfxBase -> DefaultFont;
  13.  
  14.     TextAttr -> tta_Name    = Font -> tf_Message . mn_Node . ln_Name;
  15.     TextAttr -> tta_YSize    = Font -> tf_YSize;
  16.     TextAttr -> tta_Style    = Font -> tf_Style;
  17.     TextAttr -> tta_Flags    = Font -> tf_Flags & ~FPF_REMOVED;
  18.  
  19.     if(Font -> tf_Style & FSF_TAGGED)
  20.         TextAttr -> tta_Tags = ((struct TextFontExtension *)Font -> tf_Extension) -> tfe_Tags;
  21.     else
  22.         TextAttr -> tta_Tags = NULL;
  23. }
  24.  
  25.  
  26. /*****************************************************************************/
  27.  
  28.  
  29. struct TextFont * __regargs
  30. LTP_OpenFont(struct TextAttr *TextAttr)
  31. {
  32.     struct TextFont *Font;
  33.  
  34.         // Let's see if this one is special
  35.  
  36.     if(TextAttr == (struct TextAttr *)~0)
  37.     {
  38.         struct TTextAttr LocalTextAttr;
  39.  
  40.         memset(&LocalTextAttr,0,sizeof(LocalTextAttr));
  41.  
  42.         Forbid();
  43.  
  44.             // Borrow the system default font data
  45.  
  46.         LTP_GetDefaultFont(&LocalTextAttr);
  47.  
  48.             // Let's hope the font will open...
  49.  
  50.         Font = OpenFont((struct TextAttr *)&LocalTextAttr);
  51.  
  52.         Permit();
  53.     }
  54.     else
  55.     {
  56.         if(!(Font = OpenFont(TextAttr)))
  57.         {
  58.             if(SysBase -> ThisTask -> tc_Node . ln_Type != NT_TASK)
  59.             {
  60.                 struct Library *DiskfontBase;
  61.  
  62.                 if(DiskfontBase = OpenLibrary("diskfont.library",0))
  63.                 {
  64.                     Font = OpenDiskFont(TextAttr);
  65.  
  66.                     CloseLibrary(DiskfontBase);
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     return(Font);
  73. }
  74.  
  75.  
  76. /*****************************************************************************/
  77.  
  78.  
  79. BOOLEAN __regargs
  80. LTP_GlyphSetup(struct LayoutHandle *Handle,struct TextAttr *TextAttr)
  81. {
  82.     struct TextFont *Font;
  83.  
  84.     if(TextAttr)
  85.     {
  86.         Handle -> TextAttr = TextAttr;
  87.  
  88.         if(Font = LTP_OpenFont(Handle -> TextAttr))
  89.         {
  90.             if(Handle -> CloseFont)
  91.                 CloseFont(Handle -> RPort . Font);
  92.             else
  93.                 Handle -> CloseFont = TRUE;
  94.         }
  95.     }
  96.     else
  97.     {
  98.         Handle -> TextAttr = Handle -> Screen -> Font;
  99.  
  100.         Font = Handle -> DrawInfo -> dri_Font;
  101.  
  102.         if(Handle -> CloseFont)
  103.         {
  104.             CloseFont(Handle -> RPort . Font);
  105.  
  106.             Handle -> CloseFont = FALSE;
  107.         }
  108.     }
  109.  
  110.     if(Font)
  111.     {
  112.         LONG    Count = 0;
  113.         LONG    Total = 0;
  114.         UBYTE    Glyph;
  115.         UWORD    i;
  116.  
  117.         LTP_SetFont(Handle,Font);
  118.  
  119.             // Ok, first cover the standard ASCII character set
  120.  
  121.         for(i = 32 ; i < 127 ; i++,Count++)
  122.         {
  123.             Glyph = i;
  124.  
  125.             Total += TextLength(&Handle -> RPort,&Glyph,1);
  126.         }
  127.  
  128.             // Now for the ISO part...
  129.  
  130.         for(i = 160 ; i < 256 ; i++,Count++)
  131.         {
  132.             Glyph = i;
  133.  
  134.             Total += TextLength(&Handle -> RPort,&Glyph,1);
  135.         }
  136.  
  137.         Handle -> GlyphWidth = Total / Count;
  138.  
  139.             // Now for the numeric characters
  140.  
  141.         for(Total = 0, i = '0' ; i <= '9' ; i++)
  142.         {
  143.             Glyph = i;
  144.  
  145.             Total += TextLength(&Handle -> RPort,&Glyph,1);
  146.         }
  147.  
  148.             // Actually, the numeric character should be
  149.             // just as wide as the space character. Let's
  150.             // hope for the best.
  151.  
  152.         Count = TextLength(&Handle -> RPort," ",1);
  153.  
  154.         Total /= 10;
  155.  
  156.         if(Total < Count)
  157.             Total = Count;
  158.  
  159.         if(Total > Handle -> GlyphWidth)
  160.             Handle -> GlyphWidth = Total;
  161.  
  162.         if(Handle -> GlyphWidth <= 8)
  163.             Handle -> InterWidth = 4;
  164.         else
  165.             Handle -> InterWidth = Handle -> GlyphWidth / 2;
  166.  
  167.         if(Handle -> RPort . TxHeight <= 8)
  168.             Handle -> InterHeight = 2;
  169.         else
  170.             Handle -> InterHeight = Handle -> RPort . TxHeight / 4;
  171.  
  172.         return(TRUE);
  173.     }
  174.     else
  175.         return(FALSE);
  176. }
  177.